using System;
using libssk;

namespace ssk
{
	class MainStuff
	{
		static void Main(string[] argv)
		{
			Console.WriteLine("lets start this shit");
			
			StegAction action = StegAction.None;
			byte[] obfuscationData = null;
			byte[] endKey = null;
			string primaryFilePath = string.Empty;
			string secondaryFilePath = string.Empty;
			int offset = 0;
			int i = 0;
			int blocksize = 1024;
			int headerID = 0;
			
			foreach (string s in argv)
			{
				bool printBinaryHeaders = false;
				
				if (i == 0)
				{
					
					if (s == "add")
						action = StegAction.Add;
					
					if (s == "remove")
						action = StegAction.Remove;
					
					if (s == "union")
						action = StegAction.Union;
					
					if (s == "deunion")
						action = StegAction.Deunion;
					
					if (s == "fake")
						action = StegAction.FakeHeader;
					
					if (s == "defake")
						action = StegAction.DefakeHeader;
					
					if (s == "obfuscate")
						action = StegAction.ObfuscateHeader;
					
					if (s == "deobfuscate")
						action = StegAction.DeobfuscateHeader;
					
					if (s == "list")
					{
						if (argv.Length < 2)
						{
							PrintHeaders("all");
							return;
						}
						
						printBinaryHeaders = true;
					}
					
					i++;
					continue;
				}
				
				if (i == 1)
				{
					primaryFilePath = s;
					
					Console.WriteLine("getting primary path");
					continue;
				}
				
				if ((i == 2) && (   (action == StegAction.Union) 
				                 || (action == StegAction.Add) 
				                 || (action == StegAction.Remove) 
				                 || (action == StegAction.Deunion)))
				{
					secondaryFilePath = s;
					
					Console.WriteLine("getting second path");
					continue;
				}
				else if ((i == 2) && printBinaryHeaders)
					PrintHeaders(s);
				
				if (s.StartsWith("--obfuscation-data="))
				{
					System.Text.UnicodeEncoding enc = new System.Text.UnicodeEncoding();
					string tmp = s.Replace("--obfuscation-data=", string.Empty);
					
					obfuscationData = enc.GetBytes(tmp);
				}
				
				if (s.StartsWith("--end-key="))
				{
					System.Text.UnicodeEncoding enc = new System.Text.UnicodeEncoding();
					string tmp = s.Replace("--end-key=", string.Empty);
					
					endKey = enc.GetBytes(tmp);
				}
				
				if (s.StartsWith("--offset="))
				{
					string tmp = s.Replace("--offset=", string.Empty);
					
					if (!int.TryParse(tmp, out offset))
						return;
				}
				
				if (s.StartsWith("--blocksize="))
				{
					string tmp = s.Replace("--blocksize=", string.Empty);
					
					if (!int.TryParse(tmp, out blocksize))
						return;
				}
				
				if (s.StartsWith("--header-id="))
				{
					string tmp = s.Replace("--header-id=", string.Empty);
					
					if (!int.TryParse(tmp, out headerID))
						return;
				}
				
				i++;
			}
			
			if (endKey != null && endKey.Length == 0)
				endKey = new byte[blocksize];
			
			if (obfuscationData != null && obfuscationData.Length == 0)
				obfuscationData = new byte[blocksize];
			
			StegFile primaryFile = new StegFile();
			primaryFile.FilePath = primaryFilePath;
			
			StegFile secondaryFile = null;
			
			if ((action == StegAction.Union) 
             || (action == StegAction.Add) 
             || (action == StegAction.Remove) 
             || (action == StegAction.Deunion))
			{
				secondaryFile = new StegFile();
				secondaryFile.FilePath = secondaryFilePath;
			}
			
			switch (action)
			{
				case StegAction.Add:
					primaryFile.Add(secondaryFile, offset, endKey);
					break;
				case StegAction.DefakeHeader:
					primaryFile.DefakeHeader(offset);
					break;
				case StegAction.DeobfuscateHeader:
					primaryFile.DeobfuscateHeader(obfuscationData, offset);
					break;
				case StegAction.Deunion:
					primaryFile.Deunion(offset, blocksize, endKey);
					break;
				case StegAction.FakeHeader:
					primaryFile.FakeHeader(headerID, offset); 
					break;
				case StegAction.ObfuscateHeader:
					primaryFile.ObfuscateHeader(obfuscationData, offset);
					break;
				case StegAction.Remove:
					primaryFile.Remove(offset, endKey);
					break;
				case StegAction.Union:
					primaryFile.Union(secondaryFile, offset, blocksize, endKey);
					break;
				default:
					Console.WriteLine("You didn't do something correctly...");
					break;
			}
		}
		
		static void PrintHeaders(string type)
		{
		}
	}
}

